<?php
final class CircleModel {
	private $mysql;
	private $tableName = 'Circle';
	public $idRowName = 'Circle_ID';
	public $nameRowName = 'Circle_Name';
	public $typeRowName = 'Circle_Type';
	public $CountRowName = 'Circle_Count';

	public function __construct ($mysql) {
		$this->mysql = $mysql;
	}

	public function GetCircleCount () {
		$ref = 0;
		$sqlresult = $this->mysql->query (sprintf ('SELECT COUNT(*) AS `%s` FROM `%s`;', $this->mysql->escape_string ($this->CountRowName), $this->mysql->escape_string ($this->tableName)));
		$len = $sqlresult->num_rows ();
		if ($len > 0) {
			$data = $sqlresult->fetch_array ();
			$ref = $data[$this->CountRowName];
		}
		$sqlresult->free_result ();
		return $ref;
	}

	public function CheckCircleItemExist ($ID) {
		$ref = NULL;
		$sqlresult = $this->mysql->query (sprintf ('SELECT `%s`, `%s`, `%s` FROM `%s` WHERE `%s`=\'%s\';', $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($this->nameRowName), $this->mysql->escape_string ($this->typeRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($ID)));
		$len = $sqlresult->affected_rows ();
		if ($len > 0) {
			$ref = $sqlresult->fetch_array ();
		}
		$sqlresult->free_result ();
		return $ref;
	}

	public function CheckCircleItemExistByName ($name) {
		$ref = NULL;
		$sqlresult = $this->mysql->query (sprintf ('SELECT `%s`, `%s`, `%s` FROM `%s` WHERE `%s`=\'%s\';', $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($this->nameRowName), $this->mysql->escape_string ($this->typeRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->nameRowName), $this->mysql->escape_string ($name)));
		$len = $sqlresult->affected_rows ();
		if ($len > 0) {
			$ref = $sqlresult->fetch_array ();
		}
		$sqlresult->free_result ();
		return $ref;
	}

/*	public function CheckCircleItemExistByID ($ID) {
		$ref = NULL;
		$sqlresult = $this->mysql->query (sprintf ('SELECT `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s` FROM `%s` WHERE `%s`=\'%s\';', $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($this->imeiRowName), $this->mysql->escape_string ($this->phoneNumberRowName), $this->mysql->escape_string ($this->simSerialNumberRowName), $this->mysql->escape_string ($this->imsiRowName), $this->mysql->escape_string ($this->manufacturerRowName), $this->mysql->escape_string ($this->modelRowName), $this->mysql->escape_string ($this->osRowName), $this->mysql->escape_string ($this->versionRowName), $this->mysql->escape_string ($this->remarkRowName),  $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($ID)));
		$len = $sqlresult->affected_rows ();
		if ($len > 0) {
			$ref = $sqlresult->fetch_array ();
		}
		$sqlresult->free_result ();
		return $ref;
	}
*/
	public function GetCircleItems ($offset, $count) {
		$ref = array ();
#		$sqlresult = $this->mysql->query (sprintf ('SELECT `%s`, `%s`,  `%s`,  `%s`,  `%s`, `%s`,  `%s`,  `%s`,  `%s`,  `%s` FROM `%s` LIMIT %d, %d;', $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($this->imeiRowName), $this->mysql->escape_string ($this->phoneNumberRowName), $this->mysql->escape_string ($this->simSerialNumberRowName), $this->mysql->escape_string ($this->imsiRowName), $this->mysql->escape_string ($this->manufacturerRowName), $this->mysql->escape_string ($this->modelRowName), $this->mysql->escape_string ($this->osRowName), $this->mysql->escape_string ($this->versionRowName), $this->mysql->escape_string ($this->remarkRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($offset), $this->mysql->escape_string ($count)));
		$sqlresult = $this->mysql->query (sprintf ('SELECT `%s`, `%s`,  `%s` FROM `%s` LIMIT %d, %d;', $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($this->nameRowName), $this->mysql->escape_string ($this->typeRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($offset), $this->mysql->escape_string ($count)));
		while ($data = $sqlresult->fetch_array ()) {
			$ref[] = $data;
		}
		$sqlresult->free_result ();
		return $ref;
	}

	public function DelCircleItem ($id) {
		$len = 0;
		$sqlresult = $this->mysql->query (sprintf ('DELETE FROM `%s` WHERE `%s`=\'%s\';', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($id)));
		$len = $sqlresult->affected_rows ();
		$sqlresult->free_result ();
		return $len;
	}


	public function AddCircleItem ($IMEI, $PhoneNumber, $SimSerialNumber, $IMSI, $Manufacturer, $Model, $OS, $Version, $Remark = '') {
		$sqlresult = $this->mysql->query (sprintf ('INSERT INTO `%s` (`%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`) VALUES (\'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\');', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->imeiRowName), $this->mysql->escape_string ($this->phoneNumberRowName), $this->mysql->escape_string ($this->simSerialNumberRowName), $this->mysql->escape_string ($this->imsiRowName), $this->mysql->escape_string ($this->manufacturerRowName), $this->mysql->escape_string ($this->modelRowName), $this->mysql->escape_string ($this->osRowName), $this->mysql->escape_string ($this->versionRowName), $this->mysql->escape_string ($this->remarkRowName), $this->mysql->escape_string ($IMEI), $this->mysql->escape_string ($PhoneNumber), $this->mysql->escape_string ($SimSerialNumber), $this->mysql->escape_string ($IMSI), $this->mysql->escape_string ($Manufacturer), $this->mysql->escape_string ($Model), $this->mysql->escape_string ($OS), $this->mysql->escape_string ($Version), $this->mysql->escape_string ($Remark)));
		$sqlresult->free_result ();
		return $this->mysql->insert_id ();
	}

#	public function CheckCircleItemAvailable ($ID) {
#	}



/*

// Cleanup The outdate Circle
DELETE FROM `DeviceToCircle` WHERE `DeviceToCircle_EndDateTime` < NOW();

// Cleanup The outdate Message
DELETE `Message`.* FROM `Message`
LEFT JOIN `DeviceToCircle` AS `From_DeviceToCircle`
ON (
	`Message`.`From_Circle_ID` = `From_DeviceToCircle`.`Circle_ID`
	AND
	`Message`.`Message_pubDate` > `From_DeviceToCircle`.`DeviceToCircle_StartDateTime`
	AND
	`Message`.`Message_pubDate` < `From_DeviceToCircle`.`DeviceToCircle_EndDateTime`
)
LEFT JOIN `DeviceToCircle` AS `To_DeviceToCircle`
ON (
	`Message`.`To_Circle_ID` = `To_DeviceToCircle`.`Circle_ID`
	AND
	`Message`.`Message_pubDate` > `To_DeviceToCircle`.`DeviceToCircle_StartDateTime`
	AND
	`Message`.`Message_pubDate` < `To_DeviceToCircle`.`DeviceToCircle_EndDateTime`
)
WHERE `To_DeviceToCircle`.`DeviceToCircle_ID` IS NULL OR `From_DeviceToCircle`.`DeviceToCircle_ID` IS NULL;

*/


}
?>
